import time

import requests
from lxml import etree

page = 1
while page < 5:
    # 构建当前页面的 URL
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/58.0.3029.110 Safari/537.3'
    }
    url = f"https://www.gushiwen.cn/mingjus/default.aspx?page={page}&tstr=&astr=&cstr=&xstr="
    print(url)
    try:
        req = requests.get(url, headers=header)
        # 检查请求是否成功
        req.raise_for_status()
        root = etree.HTML(req.text)

 
        poetry_list = root.xpath('//div[@class="cont"]/a[1]/text()')
        author_list = root.xpath('//div[@class="cont"]/a[2]/text()')


        for poetry, author in zip(poetry_list, author_list):
            result = poetry + " —— " + author
            print(result)

        # 递增页码
        page += 1
        time.sleep(3)
    except requests.RequestException as e:
        print(f"请求发生错误: {e}")
        break
